/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-11-25
 * Time: 0:31
 */
public class Solution {
    public int mySqrt(int x) {
        //处理边界
        if (x == 0 || x == 1) {
            return x;
        }
        int left = 0;
        int right = x;
        int ans = -1;
        while (left <= right) {
            int mid = left + (right-left)/2;
            //不强转为long可能会溢出！
            if ((long)mid*mid <= x) {
                ans = mid;
                left = mid+1;
            }else {
                right = mid-1;
            }
        }
        return ans;
    }
}
